import 'package:extended_text/extended_text.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ExpandableText extends StatefulWidget {
  final String text; //文本
  final int maxLines; //最大展示行数
  final TextStyle style; //文本样式
  final Function? tapClick;
  final String? btnOpenImg;
  final String? btnCloseImg;

  const ExpandableText(
      {super.key,
     required this.text,
        required this.maxLines,
        required this.style, this.tapClick, this.btnOpenImg, this.btnCloseImg,
      });

  @override
  _ExpandableTextState createState() => _ExpandableTextState();
}

class _ExpandableTextState extends State<ExpandableText> {
  Widget? btnOpen;
  double? btnOpenWidget;
  Widget? btnClose;
  TextAlign textAlign = TextAlign.left;
  bool expand = false;


  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, size) {
      final span = TextSpan(text: widget.text ?? '', style: widget.style);
      final tp = TextPainter(
          text: span, maxLines: widget.maxLines, textDirection: TextDirection.ltr);
      tp.layout(maxWidth: size.maxWidth);
      if (tp.didExceedMaxLines) {
        return expand
            ? RichText(
          text: TextSpan(children: <InlineSpan>[
            TextSpan(text: widget.text, style: widget.style),
            WidgetSpan(
              child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () {
                  if(widget.tapClick != null){
                    widget.tapClick!();
                    return;
                  }
                  setState(() {
                    expand = !expand;
                  });
                },
                child: Container(
                  width: 45,
                  alignment: Alignment.centerLeft,
                  child: Row(
                    children: [
                      SizedBox(width: 10.w,),
                      Image.asset(widget.btnCloseImg??'assets/common/text_up.png',width: 32.w,height: 32.w,)
                    ],
                  ),
                ),
              ),
            ),
          ]),
        )
            :  ExtendedText(
          widget.text,
          style: widget.style,
          maxLines: widget.maxLines,
          textAlign: textAlign,
          overflow: TextOverflow.ellipsis,
          overflowWidget: TextOverflowWidget(
            align: TextOverflowAlign.left,
            child: Stack(
              clipBehavior: Clip.hardEdge,
              children: [
                GestureDetector(
                  onTap: (){
                    if(widget.tapClick != null){
                      widget.tapClick!();
                      return;
                    }
                    setState(() {
                      expand = !expand;
                    });
                  },child: Container(
                  width: 45,
                  alignment: Alignment.centerLeft,
                  child: RichText(
                      textAlign: TextAlign.left,
                      text: TextSpan(
                          text: '... ',
                          style: widget.style,
                          children: <InlineSpan>[
                            WidgetSpan(child: Container(
                              child: Image.asset(widget.btnOpenImg??'assets/common/text_down.png',width: 32.w,),
                            )),
                          ])),
                ),
                ),
                // Positioned(
                //     top: -2,
                //     left: 2,
                //     child:
                // ),
              ],
            ),
          ),
        );
      } else {
        print(tp.didExceedMaxLines);
        return Text(widget.text ?? '', style: widget.style);
      }
    });
  }
}
